package com.liuyang.controller;


import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuyang.common.Result;
import com.liuyang.common.TableResult;
import com.liuyang.entity.Carouselimg;
import com.liuyang.service.CarouselimgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者:caleb
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/system/carouselimg")
public class CarouselimgController {

    @Autowired
    private CarouselimgService carouselimgService;

    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file")MultipartFile file) {
        try {
            File dir = new File("D:\\upload\\carousel");
            if(!dir.exists()) {
                dir.mkdirs();
            }
            Snowflake snowflake = new Snowflake(2,3);
            String id = snowflake.nextIdStr();
            String fileName = file.getOriginalFilename();
            fileName = id + fileName.substring(fileName.indexOf("."));

            Carouselimg carouselimg = new Carouselimg();
            carouselimg.setId(id); // 图片ID
            carouselimg.setImgName(fileName); // 图片名称
            carouselimg.setImgPath("D:\\upload\\carousel\\" + fileName); // 图片路径
            carouselimg.setImgStatus("0"); // 图片展示状态
            carouselimgService.save(carouselimg); // 保存图片

            file.transferTo(new File("D:\\upload\\carousel\\" + fileName));
        } catch(IOException e) {
            return Result.succ("图片上传失败");
        }
        return Result.succ("图片上传成功");
    }

    // 轮播图列表
    @PostMapping("/queryCarouseInfo")
    public TableResult queryCarouseInfo(int page, int limit) {
        Map<String, Object> map = carouselimgService.queryCarouseInfo(page, limit);
        Long total = (Long) map.get("total");
        List<Carouselimg> list = (List<Carouselimg>) map.get("records");
        TableResult tableResult = new TableResult(0,"", total.intValue(), list);
        return tableResult;
    }

    // 设置登录页背景图
    @PostMapping("/editCarouseToLogin")
    public Result editCarouseToLogin(String id) {
        QueryWrapper<Carouselimg> queryWrapper = new QueryWrapper<Carouselimg>();
        queryWrapper.eq("imgStatus", "2");
        Carouselimg cc = new Carouselimg();
        cc.setImgStatus("0");
        carouselimgService.saveOrUpdate(cc, queryWrapper);

        // ======================================================================//

        Carouselimg carouselimg = new Carouselimg();
        carouselimg.setId(id);
        carouselimg.setImgStatus("2");
        carouselimgService.saveOrUpdate(carouselimg);
        return Result.succ("设置成功");
    }

    // 设置轮播背景图
    @PostMapping("/editCarouse")
    public Result editCarouse(String ids) {
        QueryWrapper<Carouselimg> queryWrapper = new QueryWrapper<Carouselimg>().ne("imgStatus", "2");
        Carouselimg carouselimg = new Carouselimg();
        carouselimg.setImgStatus("0");
        carouselimgService.saveOrUpdate(carouselimg, queryWrapper);
        String[] idsList = ids.split(",");
        for(String id : idsList) {
            Carouselimg carouselimg1 = new Carouselimg();
            carouselimg1.setId(id);
            carouselimg1.setImgStatus("1");
            carouselimgService.saveOrUpdate(carouselimg1);
        }
        return Result.succ("设置成功");
    }

    // 获取轮播图片 - 状态status=1
    @PostMapping("/queryCarouseWithStatus")
    public Result queryCarouseWithStatus() {
        List<Carouselimg> imgStatus = carouselimgService.list(new QueryWrapper<Carouselimg>().eq("imgStatus", "1"));
        return Result.succ(imgStatus);
    }
}
